home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9418 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  72 lines

  1. Path: kiwi.gen.nz!alastair
  2. From: alastair@kiwi.gen.nz (Alastair Shar)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Hiding a password
  5. Date: 10 Mar 1996 16:10:46 GMT
  6. Organization: Kiwi Internet node, Auckland, New Zealand
  7. Message-ID: <4huuu6$81c@three.kiwi.gen.nz>
  8. References: <1996Feb29.224936.137160@forest> <4he620$qf2@hpbblb.bbn.hp.com> <TANMOY.96Mar4172334@qcd.lanl.gov>
  9. NNTP-Posting-Host: kiwi.gen.nz
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. : #include <stdio.h>
  13. : char * GetString_NoEcho()
  14. : {
  15. :   char *temp;
  16. :   int i=1;
  17. :   while(temp[i-1]!=13) {
  18. :     temp[i-1]=getch();  
  19. :     i++;
  20. :   }
  21. :   temp[i]=0;
  22. : }
  23. : int main(void) {
  24. :   char *pwd = GetString_NoEcho();
  25. :   printf("%s\n",pwd);
  26. :   return 0;
  27. : }
  28.  
  29. : Can you help me? Any idea what I am doing wrong? Should I change the
  30. : i-1 to i*=+5 to get it to work?
  31. Your indexing is fine if a little to complex. As c arrays start at 0, easier
  32. to initialise i as 0, and use temp[i] rather than temp[i-1]. (just
  33. curiously, where did you come up with i*=+5 ??)
  34. Your problem with getch is that the compiler has no idea where to find the
  35. function. As getch is platform specific, you wont find it in stdio.h. Under
  36. dos compilers it's usually in conio.h. Dunno where it is in Unix, you'd have
  37. to look for it in that case.
  38. Still, your main problem is *temp. Okay, so it's a pointer. But what is it
  39. pointing to? All declaring 'char *temp' does is allocate a place for an
  40. address(pointer) to a char. It wont actually allocate any memory for an
  41. array unless you hard coded it: ie char *temp="asfdasd" or whatever. (not
  42. that useful in this case)
  43. You basically have two ways to give your pointer something to point to. Either
  44. allocate some memory, or declare an array. In the first case, something like
  45. 'char *temp=malloc(21);' would allocate 20 characters for the password (the
  46. 21st for the null terminater). Unfortunatly, as your code stands, this
  47. wouln't help cause you have no means of passing the pointer to this back to
  48. your main function. By delaring 'char *GetString_NoEcho()' you imply that
  49. the function returns a char pointer, but you still need 'return(temp);' as
  50. the last line of the function to achieve this. Oh yeah, you'd also have to
  51. #include a header file that specifies malloc...
  52. Alternatively, you could declare an array in the main program, and pass it's
  53. address to the function eg 'char pwd[21]; GetString_NoEcho(pwd);' and change
  54. the function declaration to be 'void GetString_NoEcho(char temp[])'. In this
  55. case you dont need to return an address cause you already have it in the
  56. main program, and you have to remove the 'char *temp' declaration in the
  57. function.
  58. In both these cases, you have to keep a check on how many characters are
  59. entered in the password. If more than 20 are entered, you end up writing
  60. into memory that hasn't been allocated for that purpose. Sometimes nothing
  61. odd happens... then again, just as likely, you'll end up having to reset
  62. your computer.
  63.  
  64. hope this helps...
  65.  
  66.  
  67.  
  68. --------------------------------------------------------------------------
  69. It began as a regular day in my room,
  70. with a cup of hot black coffee...                           <meryn cadell>
  71. --------------------------------------------------------------------------
  72.